package com.kifas.serviceimpl;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kifas.entity.AkunMasterEntity;
import com.kifas.entity.BarangEntity;
import com.kifas.entity.BukuHutangEntity;
import com.kifas.entity.BukuPiutangEntity;
import com.kifas.entity.BukuVoucherEntity;
import com.kifas.entity.CustomerEntity;
import com.kifas.entity.GroupEntity;
import com.kifas.entity.GudangEntity;
import com.kifas.entity.HutangEntity;
import com.kifas.entity.JenisBarangEntity;
import com.kifas.entity.JenisCustomerEntity;
import com.kifas.entity.KontrakEntity;
import com.kifas.entity.KontrakPembelianEntity;
import com.kifas.entity.MenuEntity;
import com.kifas.entity.PembelianDetailEntity;
import com.kifas.entity.PembelianEntity;
import com.kifas.entity.PenerimaanDetailEntity;
import com.kifas.entity.PenerimaanEntity;
import com.kifas.entity.PengirimanEntity;
import com.kifas.entity.PenjualanDetailEntity;
import com.kifas.entity.PenjualanEntity;
import com.kifas.entity.PiutangEntity;
import com.kifas.entity.ReferenceNumberingEntity;
import com.kifas.entity.SalesEntity;
import com.kifas.entity.SatuanEntity;
import com.kifas.entity.StokOpnameEntity;
import com.kifas.entity.SupplierEntity;
import com.kifas.entity.TransactionJurnalUmumEntity;
import com.kifas.entity.TransactionStokEntity;
import com.kifas.entity.TypeAkunEntity;
import com.kifas.entity.UserEntity;
import com.kifas.service.EntityServiceManager;
import com.kifas.service.dataaccess.AkunMasterDao;
import com.kifas.service.dataaccess.BarangDao;
import com.kifas.service.dataaccess.BukuVoucherDao;
import com.kifas.service.dataaccess.CustomerDao;
import com.kifas.service.dataaccess.GroupDao;
import com.kifas.service.dataaccess.GudangDao;
import com.kifas.service.dataaccess.HutangDao;
import com.kifas.service.dataaccess.JenisBarangDao;
import com.kifas.service.dataaccess.JenisCustomerDao;
import com.kifas.service.dataaccess.KontrakDao;
import com.kifas.service.dataaccess.MenuDao;
import com.kifas.service.dataaccess.PembelianDao;
import com.kifas.service.dataaccess.PenerimaanDao;
import com.kifas.service.dataaccess.PengirimanDao;
import com.kifas.service.dataaccess.PenjualanDao;
import com.kifas.service.dataaccess.PiutangDao;
import com.kifas.service.dataaccess.ReferenceNumberingDao;
import com.kifas.service.dataaccess.SalesDao;
import com.kifas.service.dataaccess.SatuanDao;
import com.kifas.service.dataaccess.StokOpnameDao;
import com.kifas.service.dataaccess.SupplierDao;
import com.kifas.service.dataaccess.TransactionJurnalDao;
import com.kifas.service.dataaccess.TransactionStokDao;
import com.kifas.service.dataaccess.TypeAkunDao;
import com.kifas.service.dataaccess.UserDao;
import com.kifas.utils.TransactionReferencePopulator;

@Service("entityServiceManager")
@Transactional(readOnly = true)
public class EntityServiceManagerImpl implements EntityServiceManager {

	private static Logger log = Logger.getLogger(EntityServiceManagerImpl.class);

	@Autowired
	private CustomerDao customerDao;

	@Autowired
	private SalesDao salesDao;

	@Autowired
	private PenjualanDao penjualanDao;

	@Autowired
	private BarangDao barangDao;

	@Autowired
	private SatuanDao satuanDao;

	@Autowired
	private PengirimanDao pengirimanDao;

	@Autowired
	private GudangDao gudangDao;

	@Autowired
	private StokOpnameDao stokOpnameDao;

	@Autowired
	private TransactionStokDao transactionStokDao;

	@Autowired
	private ReferenceNumberingDao referenceNumberingDao;

	@Autowired
	private KontrakDao kontrakDao;

	@Autowired
	private PembelianDao pembelianDao;

	@Autowired
	private SupplierDao supplierDao;

	@Autowired
	private PenerimaanDao penerimaanDao;

	@Autowired
	private JenisBarangDao jenisBarangDao;

	@Autowired
	private UserDao userDao;

	@Autowired
	private JenisCustomerDao jenisCustomerDao;

	@Autowired
	private PiutangDao piutangDao;

	@Autowired
	private HutangDao hutangDao;

	@Autowired
	private AkunMasterDao akunMasterDao;

	@Autowired
	private BukuVoucherDao bukuVoucherDao;

	@Autowired
	private TypeAkunDao typeAkunDao;

	@Autowired
	private TransactionJurnalDao transactionJurnalDao;

	@Autowired
	private GroupDao groupDao;

	@Autowired
	private MenuDao menuDao;

	/*
	 * ReferenceNumber
	 */

	@Override
	@Transactional(readOnly = false)
	public String getReference(Integer unit, String prefix) {
		return referenceNumberingDao.getNextReference(unit, prefix);
	}

	@Override
	@Transactional(readOnly = false)
	public void persist(ReferenceNumberingEntity referenceNumbering) throws Exception {
		referenceNumberingDao.insert(referenceNumbering);
	}

	/*
	 * Menu
	 */
	public List<MenuEntity> getMenusByGroupId(Long idGroup) {
		return menuDao.getMenuByGroup(groupDao.getGroupById(idGroup));
	}
	
	@Override
	@Transactional(readOnly = false)
	public void persist(MenuEntity menu) throws Exception {
		menuDao.insert(menu);
	}

	/*
	 * Group
	 */
	@Override
	public GroupEntity getGroupById(Long id) {
		return groupDao.getGroupById(id);
	}

	@Override
	@Transactional(readOnly = false)
	public void persist(GroupEntity group) throws Exception {
		groupDao.insert(group);
	}

	@Override
	public List<GroupEntity> getGroups() {
		return groupDao.getGroups();
	}

	/*
	 * User
	 */
	@Override
	public UserEntity getUser(String username) {
		return userDao.getUserById(username);
	}

	@Override
	@Transactional(readOnly = false)
	public void persist(UserEntity user) throws Exception {
		userDao.insert(user);
	}

	@Override
	public boolean getUserValidation(String username, String password) {
		return userDao.getUserValidation(username, password);
	}

	@Override
	@Transactional(readOnly = false)
	public void update(UserEntity user) throws Exception {
		userDao.update(user);
	}

	@Override
	public List<UserEntity> getUsers() {
		return userDao.getUsers();
	}

	/*
	 * Customer
	 */

	@Override
	public List<CustomerEntity> getCustomers() {
		return customerDao.getCustomers();
	}

	@Override
	public CustomerEntity getCustomer(Long id) {
		return customerDao.getCustomerById(id);
	}

	@Override
	public void persist(CustomerEntity customer) throws Exception {
		customerDao.insert(customer);
	}

	@Override
	@Transactional(readOnly = false)
	public void update(CustomerEntity customer) throws Exception {
		customerDao.update(customer);
	}

	/*
	 * Sales
	 */

	@Override
	public List<SalesEntity> getSales() {
		return salesDao.getSales();
	}

	@Override
	public SalesEntity getSales(Long id) {
		return salesDao.getSalesById(id);
	}

	@Override
	@Transactional(readOnly = false)
	public void persist(SalesEntity sales) throws Exception {
		salesDao.insert(sales);
	}

	@Override
	@Transactional(readOnly = false)
	public void update(SalesEntity sales) throws Exception {
		salesDao.update(sales);
	}

	/*
	 * Barang
	 */

	@Override
	public List<BarangEntity> getBarangs() {
		return barangDao.getBarangs();
	}

	@Override
	public BarangEntity getBarang(Long id) {
		return barangDao.getBarangById(id);
	}

	@Override
	@Transactional(readOnly = false)
	public void persist(BarangEntity barang) throws Exception {
		barangDao.insert(barang);
	}

	@Override
	@Transactional(readOnly = false)
	public void update(BarangEntity barang) throws Exception {
		barangDao.update(barang);
	}

	/*
	 * Satuan
	 */

	@Override
	public List<SatuanEntity> getSatuans(BarangEntity barang) {
		// TODO : not implement
		return null;
	}

	@Override
	public List<SatuanEntity> getSatuans() {
		return satuanDao.getSatuans();
	}

	@Override
	public SatuanEntity getSatuan(Long id) {
		return satuanDao.getSatuanById(id);
	}

	@Override
	public void persist(SatuanEntity satuan) throws Exception {
		satuanDao.insert(satuan);
	}

	@Override
	@Transactional(readOnly = false)
	public void update(SatuanEntity satuan) throws Exception {
		satuanDao.update(satuan);
	}

	/*
	 * Stok Opname
	 */
	@Override
	public StokOpnameEntity getStokOpname(Long id) {
		return stokOpnameDao.getStokOpname(id);
	}

	@Override
	public List<StokOpnameEntity> getStokOpnames() {
		return stokOpnameDao.getStokOpnames();
	}

	@Override
	public List<StokOpnameEntity> getStokOpnameAvailable() {
		return stokOpnameDao.getStokOpnameAvailable();
	}

	@Override
	public List<StokOpnameEntity> getStokOpnameAvailable(BarangEntity barang) {
		return stokOpnameDao.getStokOpnameAvailable(barang);
	}

	@Override
	public List<StokOpnameEntity> getStokOpnameAvailable(GudangEntity gudang) {
		return stokOpnameDao.getStokOpnameAvailable(gudang);
	}

	@Override
	public List<StokOpnameEntity> getStokOpnameByBarang(BarangEntity barangEntity) {
		return stokOpnameDao.getStokOpnameByBarang(barangEntity);
	}

	@Override
	public List<StokOpnameEntity> getStokOpnameByGudang(GudangEntity gudangEntity) {
		return stokOpnameDao.getStokOpnameByGudang(gudangEntity);
	}

	@Override
	public List<StokOpnameEntity> getExpiredStok() {
		return stokOpnameDao.getExpiredStok();
	}

	@Override
	public List<StokOpnameEntity> getExpiredStokByBarang(BarangEntity barangEntity) {
		return stokOpnameDao.getExpiredStokByBarang(barangEntity);
	}

	@Override
	@Transactional(readOnly = false)
	public void persist(StokOpnameEntity stokOpnameEntity) throws Exception {
		stokOpnameDao.insert(stokOpnameEntity);
	}

	@Override
	@Transactional(readOnly = false)
	public void populateTodayStok() throws Exception {
		Calendar cal = new GregorianCalendar();
		cal.setTime(new Date());
		cal.add(Calendar.DATE, -1);
		List<TransactionStokEntity> transactionStokEntities = transactionStokDao.getCurrentMonthTransactionStoks();
		String idBarang = null;
		String idGudang = null;
		String bulanKadaluarsa = null;
		Long qty = 0L;
		boolean hasLoop = false;
		for (TransactionStokEntity transactionStok : transactionStokEntities) {
			log.debug("idbarang : " + idBarang + ", transactionIdBarang : " + transactionStok.getIdBarang());
			log.debug("idGudang : " + idGudang + ", transactionIdGudang : " + transactionStok.getIdGudang());
			log.debug("bulanKadaluarsa : " + bulanKadaluarsa + ", transactionBulanKadaluarsa : " + transactionStok.getBulanKadaluarsa());
			if (hasLoop
					&& (!transactionStok.getIdBarang().equals(idBarang) || !transactionStok.getIdGudang().equals(idGudang) || !transactionStok
							.getBulanKadaluarsa().equals(bulanKadaluarsa))) {
				BarangEntity barang = barangDao.getBarangById(Long.valueOf(idBarang));
				GudangEntity gudang = gudangDao.getGudang(Long.valueOf(idGudang));
				StokOpnameEntity stok = new StokOpnameEntity(null, barang, gudang, bulanKadaluarsa, qty.intValue(), 0, "", 0, 0, cal.getTime());
				stokOpnameDao.insert(stok);
				qty = 0L;
			}
			idBarang = transactionStok.getIdBarang();
			idGudang = transactionStok.getIdGudang();
			bulanKadaluarsa = transactionStok.getBulanKadaluarsa();
			qty = (transactionStok.isInputFlag()) ? qty + transactionStok.getQty() : qty - transactionStok.getQty();
			hasLoop = true;
		}
		if (hasLoop) {
			BarangEntity barang = barangDao.getBarangById(Long.valueOf(idBarang));
			GudangEntity gudang = gudangDao.getGudang(Long.valueOf(idGudang));
			StokOpnameEntity stok = new StokOpnameEntity(null, barang, gudang, bulanKadaluarsa, qty.intValue(), 0, "", 0, 0, cal.getTime());
			stokOpnameDao.insert(stok);
		}
	}

	/*
	 * Jenis Barang
	 */
	@Override
	@Transactional(readOnly = false)
	public void persist(JenisBarangEntity jenisBarang) throws Exception {
		jenisBarangDao.insert(jenisBarang);
	}

	@Override
	public List<JenisBarangEntity> getJenisBarangs() {
		return jenisBarangDao.getJenisBarangs();
	}

	@Override
	public JenisBarangEntity getJenisBarang(Long idJenisBarang) {
		return jenisBarangDao.getJenisBarang(idJenisBarang);
	}

	@Override
	@Transactional(readOnly = false)
	public void update(JenisBarangEntity jenisBarang) throws Exception {
		jenisBarangDao.update(jenisBarang);
	}

	/*
	 * Supplier
	 */
	@Override
	public List<SupplierEntity> getSuppliers() {
		return supplierDao.getSuppliers();
	}

	@Override
	public SupplierEntity getSupplier(Long idSupplier) {
		return supplierDao.getSupplier(idSupplier);
	}

	@Override
	@Transactional(readOnly = false)
	public void persist(SupplierEntity supplier) throws Exception {
		supplierDao.insert(supplier);
	}

	@Override
	@Transactional(readOnly = false)
	public void update(SupplierEntity supplier) throws Exception {
		supplierDao.update(supplier);
	}

	/*
	 * Gudang
	 */
	@Override
	public GudangEntity getGudang(Long idMasterGudang) {
		return gudangDao.getGudang(idMasterGudang);
	}

	@Override
	public List<GudangEntity> getGudangs() {
		return gudangDao.getGudangs();
	}

	@Override
	public void persist(GudangEntity gudang) throws Exception {
		gudangDao.insert(gudang);
	}

	@Override
	@Transactional(readOnly = false)
	public void update(GudangEntity gudang) throws Exception {
		gudangDao.update(gudang);
	}

	/*
	 * Pengiriman
	 */
	@Override
	public List<PengirimanEntity> getPengirimans() {
		return pengirimanDao.getPengirimans();
	}

	@Override
	public PengirimanEntity getPengirimanById(Long id) {
		return pengirimanDao.getPengirimanById(id);
	}

	@Override
	public PengirimanEntity getLatestPengirimanByIdPenjualan(Long idPenjualan) {
		return pengirimanDao.getLatestPengirimanByIdPenjualan(idPenjualan);
	}

	@Override
	public List<PengirimanEntity> getPengirimanByIdPenjualans(List<Long> idPenjualans) {
		return pengirimanDao.getPengirimanByIdPenjualans(idPenjualans);
	}

	@Override
	public List<PengirimanEntity> getPengirimanByIdPenjualan(Long idPenjualan) {
		return pengirimanDao.getPengirimanByIdPenjualan(idPenjualan);
	}

	@Override
	public List<PengirimanEntity> getPengirimansByDate(Date startDate, Date endDate) {
		return pengirimanDao.getPengirimansByDate(startDate, endDate);
	}

	@Override
	public List<PengirimanEntity> getUnreceivedPengirimans() {
		return pengirimanDao.getUnreceivedPengirimans();
	}

	@Override
	public List<PengirimanEntity> getReceivedPengirimans() {
		return pengirimanDao.getReceivedPengirimans();
	}

	@Override
	@Transactional(readOnly = false)
	public void persist(PengirimanEntity pengiriman) throws Exception {
		pengirimanDao.insert(pengiriman);
		penjualanDao.updatePengirimanFlag(pengiriman.getIdPenjualan());
	}

	/*
	 * Penjualan
	 */

	@Override
	public PenjualanEntity getPenjualan(Long idPenjualan) {
		return penjualanDao.getPenjualanById(idPenjualan);
	}

	@Override
	public List<PenjualanEntity> getPenjualans(CustomerEntity customer) {
		return penjualanDao.getPenjualans(customer);
	}

	@Override
	public List<PenjualanEntity> getPenjualansIsClosed(boolean isClosed) {
		return penjualanDao.getPenjualansIsClosed(isClosed);
	}

	@Override
	public List<PenjualanEntity> getPenjualans(Date start, Date end) {
		return penjualanDao.getPenjualans(start, end);
	}

	@Override
	public List<PenjualanEntity> getPenjualans() {
		return penjualanDao.getPenjualans();
	}

	@Override
	@Transactional(readOnly = false)
	public void persist(PenjualanEntity penjualan) throws Exception {
		penjualanDao.insert(penjualan);
	}

	@Override
	@Transactional(readOnly = false)
	public void updatePenjualanToClosed(Long idPenjualan) throws Exception {
		penjualanDao.updateClosedFlag(idPenjualan);
	}

	@Override
	public List<PenjualanEntity> getPenjualans(List<Long> idPenjualans) {
		return penjualanDao.getPenjualans(idPenjualans);
	}

	@Override
	public List<PenjualanEntity> getPenjualans(SalesEntity sales) {
		return penjualanDao.getPenjualans(sales);
	}

	@Override
	public List<PenjualanEntity> getUnsentPenjualans() {
		return penjualanDao.getUnsentPenjualans();
	}

	@Override
	public List<PenjualanEntity> getPenjualans(Date start, Date end, boolean sentStatus) {
		return penjualanDao.getPenjualans(start, end, sentStatus);
	}

	@Override
	public List<PenjualanDetailEntity> getPenjualanDetailEntities(PenjualanEntity penjualan) {
		return penjualanDao.getPenjualanDetailEntities(penjualan);
	}

	@Override
	public List<PenjualanEntity> getPenjualanAndDetails() {
		return penjualanDao.getPenjualanAndDetails();
	}

	@Override
	public List<PenjualanEntity> getPenjualanAndDetails(Date startDate, Date endDate) {
		return penjualanDao.getPenjualanAndDetails(startDate, endDate);
	}

	@Override
	public List<PenjualanEntity> getPenjualansJthTempo(Date start) {
		return penjualanDao.getPenjualansJthTempo(start);
	}

	/*
	 * Kontrak
	 */
	@Override
	public List<KontrakEntity> getKontraks() {
		return kontrakDao.getKontraks();
	}

	@Override
	public List<KontrakEntity> getKontraks(SupplierEntity supplier) {
		return kontrakDao.getKontraksBySupplier(supplier);
	}

	@Override
	public List<KontrakPembelianEntity> getKontrakPembelians(KontrakEntity kontrak) {
		return kontrakDao.getKontrakPembelian(kontrak);
	}

	@Override
	public KontrakEntity getKontrak(Long idKontrak) {
		return kontrakDao.getKontrak(idKontrak);
	}

	@Override
	@Transactional(readOnly = true)
	public void persist(KontrakEntity kontrak) {
		kontrakDao.insert(kontrak);
	}

	@Override
	@Transactional(readOnly = false)
	public void updateKontrakPembelianToClosed(Long idKontrakPembelian) throws Exception {
		kontrakDao.updateKontrakToClosed(idKontrakPembelian);
	}

	/*
	 * Penerimaan
	 */
	@Override
	public List<PenerimaanEntity> getPenerimaans() {
		return penerimaanDao.getPenerimaans();
	}

	@Override
	public List<PenerimaanDetailEntity> getPenerimaanDetails(PenerimaanEntity penerimaan) {
		return penerimaanDao.getPenerimaanDetails(penerimaan);
	}

	@Override
	public PenerimaanEntity getPenerimaan(Long idPenerimaan) {
		return penerimaanDao.getPenerimaan(idPenerimaan);
	}

	@Override
	@Transactional(readOnly = false)
	public void persist(PenerimaanEntity penerimaan) throws Exception {
		penerimaanDao.insert(penerimaan);
		for (PenerimaanDetailEntity detail : penerimaan.getPenerimaanDetails()) {
			TransactionStokEntity transaction = new TransactionStokEntity();
			TransactionReferencePopulator referencePopulator = new TransactionReferencePopulator();
			String hashString = referencePopulator.populatePenerimaanHash(penerimaan.getNomorPenerimaan(), penerimaan.getTanggal(),
					String.valueOf(penerimaan.getIdPembelian()), String.valueOf(detail.getBarang().getIdBarang()),
					String.valueOf(detail.getGudang().getIdGudang()), detail.getBulanKadaluarsa());
			transaction.setIdTransactionStok(null);
			transaction.setCanceled(false);
			transaction.setCount(false);
			transaction.setInputFlag(true);
			transaction.setQty(detail.getQty().longValue());
			transaction.setBulanKadaluarsa(detail.getBulanKadaluarsa());
			transaction.setTanggal(new Date());
			transaction.setReference(hashString);
			transaction.setIdBarang(String.valueOf(detail.getBarang().getIdBarang()));
			transaction.setIdGudang(String.valueOf(detail.getGudang().getIdGudang()));
			transactionStokDao.insert(transaction);
		}
	}

	/*
	 * Pembelian
	 */
	@Override
	public List<PembelianEntity> getPembelians() {
		return pembelianDao.getPembelians();
	}

	@Override
	public List<PembelianDetailEntity> getPembelianDetails(PembelianEntity pembelian) {
		return pembelianDao.getPembelianDetails(pembelian);
	}

	@Override
	public PembelianEntity getPembelian(Long idPembelian) {
		return pembelianDao.getPembelianById(idPembelian);
	}

	@Override
	@Transactional(readOnly = false)
	public void persist(PembelianEntity pembelian) throws Exception {
		pembelianDao.insert(pembelian);
	}

	@Override
	public List<PembelianEntity> getOpenPembelian() {
		return pembelianDao.getOpenPembelian();
	}

	@Override
	public List<PembelianEntity> getClosedPembelian() {
		return pembelianDao.getClosedPembelian();
	}

	@Override
	public void updatePembelianToClosed(Long idPembelian) throws Exception {
		pembelianDao.updateClosedFlag(idPembelian);
	}

	/*
	 * Transaction Stok
	 */

	@Override
	public boolean isTransactionStokByReferenceExist(String reference) {
		return transactionStokDao.isTransactionStokByReferenceExist(reference);
	}

	@Override
	public List<TransactionStokEntity> getCurrentMonthTransactionStoks() {
		return transactionStokDao.getCurrentMonthTransactionStoks();
	}

	/*
	 * Jenis Customer
	 */
	@Override
	public List<JenisCustomerEntity> getJenisCustomers() {
		return jenisCustomerDao.getJenisCustomers();
	}

	@Override
	public JenisCustomerEntity getJenisCustomer(Long idJenisCustomer) {
		return jenisCustomerDao.getJenisCustomer(idJenisCustomer);
	}

	@Override
	@Transactional(readOnly = false)
	public void persist(JenisCustomerEntity jenisCustomer) throws Exception {
		jenisCustomerDao.insert(jenisCustomer);
	}

	@Override
	@Transactional(readOnly = false)
	public void update(JenisCustomerEntity jenisCustomer) throws Exception {
		jenisCustomerDao.update(jenisCustomer);
	}

	/*
	 * Piutang
	 */

	@Override
	public List<PiutangEntity> getPiutangs() {
		return piutangDao.getPiutangs();
	}

	@Override
	public List<PiutangEntity> getPiutangs(Date start, Date end) {
		return piutangDao.getPiutangs(start, end);
	}

	@Override
	public PiutangEntity getPiutang(Long idPiutang) {
		return piutangDao.getPiutang(idPiutang);
	}

	@Override
	public List<BukuPiutangEntity> getBukuPiutangs(PiutangEntity piutang) {
		return piutangDao.getBukuPiutangs(piutang);
	}

	@Override
	public List<BukuPiutangEntity> getBukuPiutangs(boolean posting) {
		return piutangDao.getBukuPiutangs(posting);
	}

	@Override
	@Transactional(readOnly = false)
	public void persist(PiutangEntity piutang) throws Exception {
		piutangDao.insert(piutang);
	}

	@Override
	@Transactional(readOnly = false)
	public void update(PiutangEntity piutang) throws Exception {
		piutangDao.update(piutang);
	}

	@Override
	@Transactional(readOnly = false)
	public void persist(BukuPiutangEntity bukuPiutang) throws Exception {
		piutangDao.insert(bukuPiutang);
	}

	@Override
	public BukuPiutangEntity getLatestBukuPiutang(PiutangEntity piutang) {
		return piutangDao.getLatestBukuPiutang(piutang);
	}

	@Override
	public List<PiutangEntity> getPiutangs(CustomerEntity customer) {
		return piutangDao.getPiutangs(customer);
	}

	@Override
	public List<PiutangEntity> getPiutangs(int days) {
		return piutangDao.getPiutangs(days);
	}

	/*
	 * Hutang
	 */
	@Override
	public List<HutangEntity> getHutangs() {
		return hutangDao.getHutangs();
	}

	@Override
	public List<HutangEntity> getHutangs(Date start, Date end) {
		return hutangDao.getHutangs();
	}

	@Override
	public HutangEntity getHutang(Long idHutang) {
		return hutangDao.getHutang(idHutang);
	}

	@Override
	public List<BukuHutangEntity> getBukuHutangs(HutangEntity hutang) {
		return hutangDao.getBukuHutangs(hutang);
	}

	@Override
	public List<BukuHutangEntity> getBukuHutangs(boolean posting) {
		return hutangDao.getBukuHutangs(posting);
	}

	@Override
	@Transactional(readOnly = false)
	public void persist(HutangEntity hutang) throws Exception {
		hutangDao.insert(hutang);
	}

	@Override
	@Transactional(readOnly = false)
	public void update(HutangEntity hutang) throws Exception {
		hutangDao.update(hutang);
	}

	@Override
	@Transactional(readOnly = false)
	public void persist(BukuHutangEntity bukuHutang) throws Exception {
		hutangDao.insert(bukuHutang);
	}

	@Override
	public BukuHutangEntity getLatestBukuHutang(HutangEntity hutang) {
		return hutangDao.getLatestBukuHutang(hutang);
	}

	@Override
	public List<HutangEntity> getHutangs(SupplierEntity supplier) {
		return hutangDao.getHutangs(supplier);
	}

	@Override
	public List<HutangEntity> getHutangs(int days) {
		return hutangDao.getHutang(days);
	}

	/*
	 * Type akun
	 */
	@Override
	public List<TypeAkunEntity> getTypeAkuns() {
		return typeAkunDao.getTypeAkuns();
	}

	@Override
	public TypeAkunEntity getTypeAkun(Long idTypeAkun) {
		return typeAkunDao.getTypeAkun(idTypeAkun);
	}

	@Override
	@Transactional(readOnly = false)
	public void persist(TypeAkunEntity typeAkun) throws Exception {
		typeAkunDao.insert(typeAkun);
	}

	@Override
	@Transactional(readOnly = false)
	public void update(TypeAkunEntity typeAkun) throws Exception {
		typeAkunDao.update(typeAkun);
	}

	/*
	 * Buku voucher
	 */
	@Override
	public List<BukuVoucherEntity> getBukuVouchers() {
		return bukuVoucherDao.getBukuVouchers();
	}

	@Override
	public BukuVoucherEntity getBukuVoucher(Long idBukuVoucher) {
		return bukuVoucherDao.getBukuVoucher(idBukuVoucher);
	}

	@Override
	@Transactional(readOnly = false)
	public void persist(BukuVoucherEntity bukuVoucher) throws Exception {
		bukuVoucherDao.insert(bukuVoucher);
	}

	@Override
	@Transactional(readOnly = false)
	public void update(BukuVoucherEntity bukuVoucher) throws Exception {
		bukuVoucherDao.update(bukuVoucher);
	}

	/*
	 * Akun Master
	 */
	@Override
	public List<AkunMasterEntity> getAkunMasters() {
		return akunMasterDao.getAkunMasters();
	}

	@Override
	public AkunMasterEntity getAkunMaster(String kodeAkun) {
		return akunMasterDao.getAkunMaster(kodeAkun);
	}

	@Override
	@Transactional(readOnly = false)
	public void persist(AkunMasterEntity akunMasterEntity) throws Exception {
		akunMasterDao.insert(akunMasterEntity);
	}

	@Override
	@Transactional(readOnly = false)
	public void update(AkunMasterEntity akunMasterEntity) throws Exception {
		akunMasterDao.update(akunMasterEntity);
	}

	@Override
	public List<PiutangEntity> getPiutangByNoRef(String noref) {
		return piutangDao.getPiutangByNoRef(noref);
	}

	@Override
	public List<AkunMasterEntity> getAkunLeaf() {
		return akunMasterDao.getAkunLeaf();
	}

	@Override
	public List<AkunMasterEntity> getAkunLeafDebet() {
		return akunMasterDao.getAkunLeafDebet();
	}

	@Override
	public List<AkunMasterEntity> getAkunLeafKredit() {
		return akunMasterDao.getAkunLeafKredit();
	}

	/*
	 * Transaction Jurnal
	 */

	@Transactional(readOnly = false)
	@Override
	public void persist(TransactionJurnalUmumEntity transactionJurnal) throws Exception {
		transactionJurnalDao.insert(transactionJurnal);
	}

	@Override
	public List<TransactionJurnalUmumEntity> getTransactionJurnals(Date start, Date end) {
		return transactionJurnalDao.getTransactions(start, end);
	}

	@Override
	public void markBukuPiutangAsPost(Long idBukuPiutang) throws Exception {
		BukuPiutangEntity bp = piutangDao.getBukuPiutang(idBukuPiutang);
		bp.setPosting("1");
		piutangDao.update(bp);
	}

	@Override
	public void markBukuHutangAsPost(Long idBukuHutang) throws Exception {
		BukuHutangEntity bh = hutangDao.getBukuHutang(idBukuHutang);
		bh.setPosting("1");
		hutangDao.update(bh);
	}

	@Override
	public void markBukuVoucherAsPost(Long idBukuVoucher) throws Exception {
		BukuVoucherEntity bv = bukuVoucherDao.getBukuVoucher(idBukuVoucher);
		bv.setPosting("1");
		bukuVoucherDao.update(bv);
	}

}